package org.hbkj.springbootmybatis.controller;

import org.hbkj.springbootmybatis.entity.AdminDo;
import org.hbkj.springbootmybatis.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 管理员登录接口
     * @param loginAdmin 包含用户名和密码的请求体
     * @return 登录结果，包含状态码和提示信息，登录成功时携带管理员信息
     */
    @PostMapping("/login")
    public Map<String, Object> adminLogin(@RequestBody AdminDo loginAdmin) {
        Map<String, Object> result = new HashMap<>();

        // 参数校验
        if (loginAdmin.getUsername() == null || loginAdmin.getUsername().trim().isEmpty()) {
            result.put("code", 400);
            result.put("msg", "用户名不能为空");
            return result;
        }

        if (loginAdmin.getPassword() == null || loginAdmin.getPassword().trim().isEmpty()) {
            result.put("code", 400);
            result.put("msg", "密码不能为空");
            return result;
        }

        AdminDo adminDo = adminService.adminLogin(loginAdmin.getUsername(), loginAdmin.getPassword());
        if (adminDo != null) {
            result.put("code", 200);
            result.put("msg", "登录成功");
            result.put("adminInfo", adminDo);
        } else {
            result.put("code", 401);
            result.put("msg", "用户名或密码错误");
        }
        return result;
    }
}